package com.xiaoyu.lanling.feature.coin.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.emoji.widget.EmojiTextView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.coin.CoinExchangeListItemButtonClickEvent
import com.xiaoyu.lanling.feature.coin.model.CoinExchangeItem
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.util.TextViewUtil

class CoinExchangeProductViewHolder : ViewHolderBase<CoinExchangeItem>() {

    companion object {
        val sOnItemClickListener = View.OnClickListener {
            val item = it.getViewTag<CoinExchangeItem>() ?: return@OnClickListener
            val activity = App.getInstance().topActivity ?: return@OnClickListener

            CoinExchangeListItemButtonClickEvent(item).post()
        }
    }

    private var mIcon: SimpleDraweeView? = null
    private var mTag: EmojiTextView? = null
    private var mTitle: EmojiTextView? = null
    private var mDesc: EmojiTextView? = null
    private var mButton: Button? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_coin_exchange, parent, false)
        mIcon = view.findViewById(R.id.icon)
        mTag = view.findViewById(R.id.tag)
        mTitle = view.findViewById(R.id.title)
        mDesc = view.findViewById(R.id.desc)
        mButton = view.findViewById(R.id.button)

        mButton?.setOnClickDebounceListener(sOnItemClickListener)

        return view
    }

    override fun showData(position: Int, itemData: CoinExchangeItem) {
        TextViewUtil.displayOrGone(mTag, itemData.tag)
        ImageViewUtil.loadImage(mIcon, ImageLoadParam.newBuilder().setUrl(itemData.image).setWidthInDP(124).setHeightInDP(60).build())
        mTitle?.text = itemData.title
        mDesc?.text = AppContext.getString(R.string.coin_exchange_list_item_point_desc, itemData.costPoint)
        mButton.setViewTag(itemData)
    }
}